﻿// CMotorDlg.cpp: 实现文件
//

#include "pch.h"
#include "SP.h"
#include "CMotorDlg.h"
#include "afxdialogex.h"


// CMotorDlg 对话框

IMPLEMENT_DYNAMIC(CMotorDlg, CDialogEx)

CMotorDlg::CMotorDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_MOTOR, pParent)
{

}

CMotorDlg::~CMotorDlg()
{
}

void CMotorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_MOTOR, m_comboAxisNum);
	DDX_Control(pDX, IDC_EDIT_POSITION, m_editCrtPos);
	DDX_Control(pDX, IDC_EDIT_MOVE_SPEED, m_editSpeedMove);
	DDX_Control(pDX, IDC_EDIT_HOME_SPEED, m_editSpeedHome);
	DDX_Control(pDX, IDC_EDIT_ACCSPEED, m_editSpeedAcc);
	DDX_Control(pDX, IDC_EDIT_STEP_DIS, m_editReDis);
	DDX_Control(pDX, IDC_CHECK_SERVO, m_btnServoOn);
	DDX_Control(pDX, IDC_CHECK_HOME, m_btnHome);
	DDX_Control(pDX, IDC_CHECK_LIMITP, m_btnLmtP);
	DDX_Control(pDX, IDC_CHECK_LIMITM, m_btnLmtN);
	DDX_Control(pDX, IDC_CHECK_ALARM, m_btnAlarm);
	DDX_Control(pDX, IDC_CHECK_RDY, m_btnRDY);
	DDX_Control(pDX, IDC_CHECK_INP, m_btnINP);
	DDX_Control(pDX, IDC_BTN_SERVO_ON, m_bBtnServoOn);
	DDX_Control(pDX, IDC_EDIT_START_SPEED, m_editStartSpeed);
	DDX_Control(pDX, IDC_COMBO_DIRECTION, m_comboDirection);
	DDX_Control(pDX, IDC_EDIT_STEP_DIS2, m_target);
}

BOOL CMotorDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	SetTimer(TIMER_ID_AXIS_STATUS, 500, NULL);

	m_mapAxisSetting = *Config.GetAxisSettingDict();
	ListAllAxis();
	ShowCrtAxisMsg();

	
	return TRUE;
}


BEGIN_MESSAGE_MAP(CMotorDlg, CDialogEx)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_STOP, &CMotorDlg::OnBnClickedBtnStop)
	ON_BN_CLICKED(IDC_BUTTON_MOVE, &CMotorDlg::OnBnClickedButtonMove)
	ON_BN_CLICKED(IDC_BTN_SAVE, &CMotorDlg::OnBnClickedBtnSave)
	ON_BN_CLICKED(IDC_BUTTON_RESET_ALARM, &CMotorDlg::OnBnClickedButtonResetAlarm)
	ON_BN_CLICKED(IDC_BUTTON_STEP_P, &CMotorDlg::OnBnClickedButtonStepP)
	ON_BN_CLICKED(IDC_BUTTON_STEP_M, &CMotorDlg::OnBnClickedButtonStepM)
	ON_CBN_SELCHANGE(IDC_COMBO_MOTOR, &CMotorDlg::OnCbnSelchangeComboMotor)
	ON_BN_CLICKED(IDC_BUTTON_HOME, &CMotorDlg::OnBnClickedButtonHome)
	ON_BN_CLICKED(IDC_BUTTON_ABS, &CMotorDlg::OnBnClickedButtonAbs)
	ON_BN_CLICKED(IDC_BTN_SERVO_ON, &CMotorDlg::OnBnClickedBtnServoOn)
	ON_EN_CHANGE(IDC_EDIT_ACCSPEED, &CMotorDlg::OnEnChangeEditAccspeed)
	ON_BN_CLICKED(IDC_CHECK_SERVO, &CMotorDlg::OnBnClickedCheckServo)
END_MESSAGE_MAP()


// CMotorDlg 消息处理程序


void CMotorDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (nIDEvent== TIMER_ID_AXIS_STATUS)
	{
		GetCrtAxisStatus();
	}
	CDialogEx::OnTimer(nIDEvent);
}

void CMotorDlg::ListAllAxis()
{
	/*m_comboDirection.ResetContent();
	m_comboDirection.AddString(L"反向");
	m_comboDirection.AddString(L"正向");
	m_comboDirection.SetCurSel(1);

	int nCrtDir = m_comboDirection.GetCurSel();

	m_comboAxisNum.ResetContent();*/
	for (auto it = m_mapAxisSetting.begin(); it != m_mapAxisSetting.end(); it++)
	{
		CString cstr;
		//cstr.Format(L"%d", it->first);
		switch (it->first) {
		case 0:
			cstr.Format(L"%d:AA X", it->first);
			break;
		case 1:
			cstr.Format(L"%d:AA Y", it->first);
			break;
		case 2:
			cstr.Format(L"%d:AA Z", it->first);
			break;
		default:
			cstr.Format(L"%d", it->first);
			break;
		}
		m_comboAxisNum.AddString(cstr);
	}
	m_comboAxisNum.SetCurSel(0);
	
	m_crtAxisSetting = m_mapAxisSetting[0];
}

void CMotorDlg::ShowCrtAxisMsg()
{
	CString cstr;
	cstr.Format(L"%d", m_crtAxisSetting.m_nSpeedHome);
	m_editSpeedHome.SetWindowTextW(cstr);

	cstr.Format(L"%d", m_crtAxisSetting.m_nSpeedMove);
	m_editSpeedMove.SetWindowTextW(cstr);

	cstr.Format(L"%.2f", m_crtAxisSetting.m_nSpeedAcc);
	m_editSpeedAcc.SetWindowTextW(cstr);

	cstr.Format(L"%d", m_crtAxisSetting.m_nStartSpeed);
	m_editStartSpeed.SetWindowTextW(cstr);

	m_editCrtPos.SetWindowTextW(L"0");
	m_editReDis.SetWindowTextW(L"0");
}

void CMotorDlg::GetCrtAxisStatus()
{
	m_btnServoOn.SetCheck(!Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bServoOn);
	m_btnAlarm.SetCheck(Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bALM);
	m_btnLmtP.SetCheck(Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bPEL);
	m_btnLmtN.SetCheck(Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bNEL);
	m_btnHome.SetCheck(Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bORG);
	m_btnRDY.SetCheck(Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bRDY);
	m_btnINP.SetCheck(Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bINP);

	m_bBtnServoOn.SetWindowTextW(Manager.m_vCMotor[m_comboAxisNum.GetCurSel()].m_bServoOn ? L"Servo_ON" : L"Servo_OFF");
	long nDis = Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_nCurPositon;
	CString cstr;
	cstr.Format(L"%d", nDis);
	m_editCrtPos.SetWindowTextW(cstr);
}


void CMotorDlg::OnBnClickedBtnStop()
{
	// TODO: 在此添加控件通知处理程序代码
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].StopMove();
}


void CMotorDlg::OnBnClickedButtonMove()
{
	// TODO: 在此添加控件通知处理程序代码
	int dir = m_comboDirection.GetCurSel();
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].MotorMoveContinue(dir);
}


void CMotorDlg::OnBnClickedBtnSave()
{
	// TODO: 在此添加控件通知处理程序代码
	CString cstrId;
	//m_comboAxisNum.GetWindowTextW(cstrId);
	int nCrtIndex = m_comboAxisNum.GetCurSel();
	CString cstr;

	m_editSpeedHome.GetWindowTextW(cstr);
	int speedhome = _ttoi(cstr);
	if (speedhome > 200000)
	{
		cstr = L"200000";
	}
	m_crtAxisSetting.m_nSpeedHome = _ttoi(cstr);
	m_editSpeedMove.GetWindowTextW(cstr);
	int speedmove = _ttoi(cstr);
	if (speedmove > 200000)
	{
		cstr = L"200000";
	}
	m_crtAxisSetting.m_nSpeedMove = _ttoi(cstr);
	m_editSpeedAcc.GetWindowTextW(cstr);
	int speedacc = _ttoi(cstr);
	if (speedacc > 200000)
	{
		cstr = L"200000";
	}
	m_crtAxisSetting.m_nSpeedAcc = _ttof(cstr);
	m_editStartSpeed.GetWindowTextW(cstr);
	int startspeed = _ttoi(cstr);
	if (startspeed > 200000)
	{
		cstr = L"200000";
	}
	m_crtAxisSetting.m_nStartSpeed = _ttoi(cstr);
	cstrId.Format(L"%d", nCrtIndex);
	Config.UpdateAxisSetting(cstrId.GetBuffer(), m_crtAxisSetting);

	m_mapAxisSetting = *Config.GetAxisSettingDict();
	Manager.GetCMotor()->ReadConfigFile();
	ShowCrtAxisMsg();
	AfxMessageBox(L"参数保存成功");
}


void CMotorDlg::OnBnClickedButtonResetAlarm()
{
	// TODO: 在此添加控件通知处理程序代码
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].ResetAlarm();
}


void CMotorDlg::OnBnClickedButtonStepP()
{
	// TODO: 在此添加控件通知处理程序代码
	//Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId];
	CString cstr;
	m_editReDis.GetWindowTextW(cstr);
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_sAxisSetting.m_dTargetPos = _ttof(cstr);
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].MotorStepMove();

}


void CMotorDlg::OnBnClickedButtonStepM()
{
	// TODO: 在此添加控件通知处理程序代码
	CString cstr;
	m_editReDis.GetWindowTextW(cstr);
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_sAxisSetting.m_dTargetPos = -_ttof(cstr);
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].MotorStepMove();
}


void CMotorDlg::OnCbnSelchangeComboMotor()
{
	// TODO: 在此添加控件通知处理程序代码
	int nCrtIndex = m_comboAxisNum.GetCurSel();
	m_crtAxisSetting = m_mapAxisSetting[nCrtIndex];

	CString cstr;
	cstr.Format(L"%d", m_crtAxisSetting.m_nSpeedHome);
	m_editSpeedHome.SetWindowTextW(cstr);
	cstr.Format(L"%d", m_crtAxisSetting.m_nSpeedMove);
	m_editSpeedMove.SetWindowTextW(cstr);
	cstr.Format(L"%.2f", m_crtAxisSetting.m_nSpeedAcc);
	m_editSpeedAcc.SetWindowTextW(cstr);
	cstr.Format(L"%d", m_crtAxisSetting.m_nStartSpeed);
	m_editStartSpeed.SetWindowTextW(cstr);
	ShowCrtAxisMsg();

}


void CMotorDlg::OnBnClickedButtonHome()
{
	// TODO: 在此添加控件通知处理程序代码
	int Direction=m_comboDirection.GetCurSel();
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].HomeSearch(Direction);
}

void CMotorDlg::OnBnClickedButtonAbs()
{
	// TODO: 在此添加控件通知处理程序代码
	CString cstr;
	m_target.GetWindowTextW(cstr);
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_sAxisSetting.m_dTargetPos = _ttof(cstr);
	Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].MotorMoveAbs();
}


void CMotorDlg::OnBnClickedBtnServoOn()
{
	// TODO: 在此添加控件通知处理程序代码
	if (!Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bServoOn)
	{
		m_bBtnServoOn.SetWindowTextW(L"Servo_On");
	}
	else
	{
		m_bBtnServoOn.SetWindowTextW(L"Servo_Off");
	}

	if (!Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].m_bServoOn)
	{
		Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].ServoOff();

	}
	else
	{
		Manager.m_vCMotor[m_crtAxisSetting.m_nAxisId].ServoOn();
	}
}


void CMotorDlg::OnEnChangeEditAccspeed()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
}


void CMotorDlg::OnBnClickedCheckServo()
{
	// TODO: 在此添加控件通知处理程序代码
}
